package com.naodong.shenluntiku.mvp.view.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.naodong.shenluntiku.R;
import com.naodong.shenluntiku.di.component.DaggerCollectOperateComponent;
import com.naodong.shenluntiku.di.module.CollectOperateModule;
import com.naodong.shenluntiku.mvp.contract.CollectOperateContract;
import com.naodong.shenluntiku.mvp.model.bean.CollectType;
import com.naodong.shenluntiku.mvp.model.bean.EventBusTag;
import com.naodong.shenluntiku.mvp.model.bean.SubjectInfo;
import com.naodong.shenluntiku.mvp.model.data.manager.BoxManager;
import com.naodong.shenluntiku.mvp.presenter.CollectOperatePresenter;
import com.naodong.shenluntiku.mvp.view.fragment.SubjectInfoFragment;
import com.yatatsu.autobundle.AutoBundleField;

import butterknife.BindView;
import me.shingohu.man.base.BaseMVPActivity;
import me.shingohu.man.di.component.AppComponent;
import me.shingohu.man.intergation.EventManager;
import me.shingohu.man.model.bean.SimpleEvent;

/**
 * Created by hujie on 17/5/23.
 */

public class SubjectInfoActivity extends BaseMVPActivity<CollectOperatePresenter> implements CollectOperateContract.View {
    @AutoBundleField
    SubjectInfo subjectInfo;
    @BindView(R.id.menuView)
    LinearLayout menuView;
    ImageView collectView;


    private void addRightCollectView() {
        collectView = (ImageView) LayoutInflater.from(this).inflate(R.layout.menu_image, null);
        menuView.addView(collectView);
        menuView.setVisibility(View.VISIBLE);
        collectView.setBackgroundResource(R.drawable.collect_selector);

        collectView.setSelected(BoxManager.getInstance().queryBoxCollectStatusByIdAndType(subjectInfo.getuSubId(), 1));
        menuView.setOnClickListener(v -> {
            mPresenter.requestPostCollect(subjectInfo.getuSubId(), CollectType.SUBJECTTYPE_ID, BoxManager.getInstance().queryBoxCollectStatusByIdAndType(subjectInfo.getuSubId(), 1));
        });
    }

    @Override
    protected void onCreateInit(Bundle savedInstanceState) {
        addRightCollectView();
        loadRootFragment(R.id.container, SubjectInfoFragment.getInstance(true, subjectInfo));
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerCollectOperateComponent.builder().appComponent(appComponent).collectOperateModule(new CollectOperateModule(this)).build().inject(this);
    }

    @Override
    protected boolean userAutoBindBundle() {
        return true;
    }

    @Override
    protected int layoutId() {
        return R.layout.a_subject_info;
    }

    @Override
    public void showMessage(String msg) {

    }

    @Override
    public void onRequesrStart() {

    }

    @Override
    public void onRequestFinish() {

    }

    @Override
    public void onCollectSuccess(int resourceId, boolean status) {
        collectView.setSelected(status);
    }


    @Override
    public void finish() {
        checkCancleCollect();
        super.finish();
    }

    public void checkCancleCollect() {
        if (!BoxManager.getInstance().queryBoxCollectStatusByIdAndType(subjectInfo.getuSubId(), CollectType.SUBJECTTYPE_ID)) {
            SimpleEvent event = new SimpleEvent(EventBusTag.NOTIFY_CANCLE_COLLECT, subjectInfo.getuSubId() + "", CollectType.SUBJECTTYPE_ID);
            EventManager.getInstance().post(event);
        }
    }
}
